package com.stars.icp.utils;

import java.io.Serializable;

/**
 * 响应信息主体
 *
 * @author ruoyi
 */
public class R<T> implements Serializable
{
    private static final long serialVersionUID = 1L;

    /** 成功 */
    public static final int SUCCESS = 200;

    /** 失败 */
    public static final int FAIL = 500;

    /**
     * 状态码
     */
    private int code;

    /**
     * 说明
     */
    private String msg;

    /**
     * 数据
     */
    private T data;

    /**
     * 总条数
     */
    private Long total;

    public static <T> R<T> ok()
    {
        return restResult(null,null, SUCCESS, null);
    }

    public static <T> R<T> ok(T data)
    {
        return restResult(data,0L, SUCCESS, null);
    }

    public static <T> R<T> ok(T data,Long total){
        return restResult(data,total,SUCCESS,null);
    }

    public static <T> R<T> ok(T data, String msg)
    {
        return restResult(data,0L, SUCCESS, msg);
    }

    public static <T> R<T> fail()
    {
        return restResult(null,0L, FAIL, null);
    }

    public static <T> R<T> fail(String msg)
    {
        return restResult(null,0L, FAIL, msg);
    }

    public static <T> R<T> fail(T data)
    {
        return restResult(data,0L, FAIL, null);
    }

    public static <T> R<T> fail(T data, String msg)
    {
        return restResult(data, 0L,FAIL, msg);
    }

    public static <T> R<T> fail(int code, String msg)
    {
        return restResult(null,0L, code, msg);
    }

    private static <T> R<T> restResult(T data, Long total,int code, String msg)
    {
        R<T> apiResult = new R<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setTotal(total);
        apiResult.setMsg(msg);
        return apiResult;
    }

    public static <T> Boolean isError(R<T> ret)
    {
        return !isSuccess(ret);
    }

    public static <T> Boolean isSuccess(R<T> ret)
    {
        return R.SUCCESS == ret.getCode();
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Long getTotal() {
        return total;
    }

    public void setTotal(Long total) {
        this.total = total;
    }
}
